package com.hnust.service.impl;

import com.hnust.dao.CollegeAdminDao;
import com.hnust.dao.SchoolAdminDao;
import com.hnust.domain.CollegeAdmin;
import com.hnust.domain.SchoolAdmin;
import com.hnust.service.TokenService;
import com.hnust.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service("tokenService")
public class TokenServiceImpl implements TokenService {

    @Autowired
    private CollegeAdminDao collegeAdminDao;

    @Autowired
    private SchoolAdminDao schoolAdminDao;

    /**
     * 验证token
     * @param token token信息
     * @param identity 身份信息
     * @return
     */
    @Override
    public Boolean verifyToken(String token, String identity) {
        //判断身份，出现异常直接不通过
        if(identity.equals("院管理员")){
            try{
                //验证信息，去dao中查询
                Map<String,String> map=new HashMap<>();
                map=JwtToken.verifyToken(token);
                CollegeAdmin collegeAdmin=collegeAdminDao.findCollegeAdmin(map.get("id"),map.get("password"));
                //不为空则验证通过
                return collegeAdmin != null;
            }catch (Exception e){
                return false;
            }
        }else{
            try{
                //验证信息，去dao中查询
                Map<String,String> map=new HashMap<>();
                map=JwtToken.verifyToken(token);
                SchoolAdmin schoolAdmin=schoolAdminDao.findSchoolAdmin(map.get("id"),map.get("password"));
                //不为空则验证通过
                return schoolAdmin != null;
            }catch (Exception e){
                return false;
            }
        }

    }

    @Override
    public String getAdminAccount(String token) {
        try{
            Map<String,String> map=new HashMap<>();
            map=JwtToken.verifyToken(token);
            return map.get("id");
        }catch(Exception e){
            return null;
        }

    }
}
